package com.freeland.client2009.webservices.impl;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.freeland.client2009.common.Constant;
import com.freeland.client2009.common.UserType;
import com.freeland.client2009.dao.ibatis.model.GameInfo;
import com.freeland.client2009.service.CommonService;
import com.freeland.client2009.service.GameInfoService;
import com.freeland.client2009.webservices.GameService;
import com.freeland.core.utils.MultiIdUtils;

@WebService(endpointInterface="com.freeland.client2009.webservices.GameService")
@Component("gameServiceImpl")
public class GameServiceImpl implements GameService {

	@Autowired
	private CommonService commonService;
	@Autowired
	private GameInfoService gameInfoService;
	
	
	public String getGameInfoList(String cooperationId) {
		//检查合作商ID
		String iRet = "-9999" ;
		if (commonService.hasCooperation(cooperationId)){
			GameInfo paraObj = new GameInfo();
			
			List<GameInfo> gameInfoList = gameInfoService.getGameInfoList(paraObj);
			
			iRet = buildGameInfoXml(gameInfoList);
			
		} else {
			iRet = "-1";
		}
		return iRet;
	}
	/**
	 * 获取该地区之前的游戏推送设置列表
	 * @param sCooperationId 合作商ID
	 * @param sAreaId 地区ID
	 * @return
	 */
	public String  getPushedGameInfoList(String cooperationId,String areaId,String netBarId){
	
		String iRet = "-9999" ;
		
		//检查合作商ID
		if (commonService.hasCooperation(cooperationId)){
			//检查地区id
			if(StringUtils.hasText(areaId) || StringUtils.hasText(netBarId)){
				//根据合作商id来判断用户的类型
				String userType =UserType.USER_EROOM.value();
				if(Constant.COOPERATION_XC.equals(cooperationId)){
					userType=UserType.USER_XC.value();
				}
				
				List<GameInfo> gameInfoList = gameInfoService.getPushedGameInfoList(areaId,netBarId,userType);
				
				iRet = buildGameInfoXml(gameInfoList);
			}else{
				iRet="-2";//地区ID和酒店（网吧)不能都为空
			}
		} else {
			iRet = "-1";//合作商ID不存在
		}
		return iRet;
		
	}

	public int setGameInfoProperty(String cooperationId, String gameId) {
		//检查合作商ID
		int iRet = -9999 ;
		if (commonService.hasCooperation(cooperationId)){
			if(MultiIdUtils.matchMultiId(gameId)){
				GameInfo paraObj = new GameInfo();
				paraObj.setMultiGameid(gameId);
				paraObj.setGameeAcessAttr(new BigDecimal(1));
                
				if (gameInfoService.updateGameInfo(paraObj) == 1){
					iRet = 0; //修改成功
				} 
			} else {
				iRet = -2; //游戏ID格式不合法
			}
		} else {
			iRet = -1; //合作商ID不存在
		}
		return iRet;
	}
	
	private String buildGameInfoXml(List<GameInfo> gameInfoList) {
		String iRet;
		iRet = "<gameinfos>{0}</gameinfos>";
		StringBuilder sbGameInfos = new StringBuilder();
		String gameinfoTemplate = "<gameinfo><gameid>{0}</gameid><gamename>{1}</gamename><gametype>{2}</gametype><createtime>{3}</createtime><releasetime>{4}</releasetime><gamecompany>{5}</gamecompany><gamesize>{6}</gamesize></gameinfo>";
		
		for(GameInfo gameInfo : gameInfoList){
			sbGameInfos.append(MessageFormat.format(gameinfoTemplate,new Object[]{gameInfo.getGameid().toString(),gameInfo.getGamename(),gameInfo.getGamecatagory(),gameInfo.getCreatetime().toString(),gameInfo.getReleasetime().toString(),gameInfo.getCompanyname(),gameInfo.getGamesize().toString()}));
		}
		iRet = MessageFormat.format(iRet,sbGameInfos.toString());
		return iRet;
	}
	public String getDeletedGameInfoList(String cooperationId, String areaId,
			String netBarId) {
		String iRet = "-9999" ;
		
		//检查合作商ID
		if (commonService.hasCooperation(cooperationId)){
			//检查地区id
			if(StringUtils.hasText(areaId) || StringUtils.hasText(netBarId)){
				//根据合作商id来判断用户的类型
				String userType =UserType.USER_EROOM.value();
				if(Constant.COOPERATION_XC.equals(cooperationId)){
					userType=UserType.USER_XC.value();
				}
				
				List<GameInfo> gameInfoList = gameInfoService.getDeletedGameInfoList(areaId,netBarId,userType);
				
				iRet = buildGameInfoXml(gameInfoList);
			}else{
				iRet="-2";//地区ID和酒店（网吧)不能都为空
			}
		} else {
			iRet = "-1";//合作商ID不存在
		}
		return iRet;
		
	}

}
